package com.fowo.api.sys.controller;

import com.fowo.api.common.model.R;
import com.fowo.api.sys.service.SysDevService;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 开发专用接口，只在 dev 环境生效
 * @author yl_ls
 */
@Profile("dev")
@RequestMapping("/api/sys/dev")
@RestController
public class SysDevController {
    @Resource
    private SysDevService service;

    @GetMapping
    public R<?> ping() {
        return R.ok(true);
    }

    @GetMapping("scripts")
    public R<List<String>> getScriptNames() {
        return R.data(service::getScriptNames);
    }

    @GetMapping("scripts/{name}")
    public R<?> getScript(@PathVariable String name) {
        return R.data(() -> service.getScriptByName(name));
    }

    @PutMapping("scripts/{name}")
    public R<?> updateScript(@PathVariable String name, @RequestBody R<String> req) {
        return R.run(() -> service.updateScriptByName(name, req.getData()));
    }

    @DeleteMapping("scripts/{name}")
    public R<?> removeScript(@PathVariable String name) {
        return R.run(() -> service.removeScript(name));
    }
}
